<!--  -->
<template>
  <div class="warp">
    <a-carousel ref="carousel" arrows :afterChange="handleCarouselChange">
      <div slot="prevArrow" slot-scope="props" class="custom-slick-arrow" style="left: 10px; z-index: 1">
        <a-icon type="left-circle" />
      </div>
      <div slot="nextArrow" slot-scope="props" class="custom-slick-arrow" style="right: 10px">
        <a-icon type="right-circle" />
      </div>
      <div class="banner" v-for="(item, index) in bannerList" :key="item.id" @click="editBanner(item)">
        <img class="img" :src="item.imgUrl" />
        <img class="del" src="@/assets/icons/del.png" alt="" @click.stop="handleDel(item.id, index)" />
        <!-- <a-icon type="delete" class="del" style="fontSize: 18px" @click="handleDel(item.id, index)" /> -->
      </div>
      <div @click="addBanner">
        <a-icon type="plus" style="fontsize: 60px" />
      </div>
    </a-carousel>
    <!-- <a-form-model :model="form" :label-col="labelCol" :wrapper-col="wrapperCol" style="margin-top: 10px">
      <a-form-model-item label="页面跳转路径：" labelAlign="left">
        <SelectOps
          :selectType="selectType"
          :terminalType="terminalType"
          :disabled="!form.id"
          :typeOptionsId="typeOptionsId"
          :optionsId="optionsId"
          :optionsName="optionsName"
          :linkUrl="linkUrl"
          :appId="appId"
          @setSelectData="getSelectData"
        />
      </a-form-model-item>
    </a-form-model> -->
    <a-button style="margin-top: 30px;" type="primary" @click="addBanner">新增轮播</a-button>
    <AddBanner
      ref="addBanner"
      :terminalType="terminalType"
      :selectType="selectType"
      :desc="terminalType === 1 ? '360*180' : '2000*901'"
      @success="addSuccess"
    />
  </div>
</template>

<script>
import AddBanner from '../addBanner'
import SelectOps from '../selectOps'
import { getConfigBanner, editConfigBanner, delConfigBanner } from '@/api/web/web'
export default {
  props: ['terminalType', 'selectType'],
  components: {
    AddBanner,
    SelectOps,
  },
  data() {
    return {
      labelCol: { span: 5 },
      wrapperCol: { span: 14 },
      bannerList: [],
      form: {
        id: undefined,
        imgUrl: undefined,
      },
      typeOptionsId: undefined,
      optionsId: undefined,
      optionsName: undefined,
      linkUrl: undefined,
      appId: undefined,
    }
  },
  computed: {},
  watch: {},
  methods: {
    addBanner() {
      this.$refs.addBanner.show()
    },
    editBanner(item) {
      this.$refs.addBanner.show(item)
    },
    handleSubmit() {},
    async get_config_banner() {
      let res = await getConfigBanner({ terminalType: this.terminalType })
      this.bannerList = res.data.records || []
      // this.$refs.carousel.goTo(0, true)
    },
    async getSelectData(data) {
      console.log('data--->', data)
      let { code, msg } = await editConfigBanner({ ...data, ...this.form })
      this.$message[code === 0 ? 'success' : 'error'](msg)
      if (code === 0) {
        this.get_config_banner()
      }
    },
    async addSuccess() {
      await this.get_config_banner()
      this.$refs.carousel.goTo(this.bannerList.length - 1, true)
      this.handleCarouselChange(this.bannerList.length - 1)
    },
    //轮播图切换后回调
    handleCarouselChange(current) {
      let item = this.bannerList[current]
      if (!item) {
        this.form.id = undefined
        return
      }
      this.form.id = item.id
      this.form.imgUrl = item.imgUrl
      this.typeOptionsId = item.typeOptionsId
      this.optionsId = item.optionsId
      this.optionsName = item.optionsName
      this.linkUrl = item.linkUrl
      this.appId = item.appId
    },
    //删除轮播图
    async handleDel(id, index) {
      var _this = this
      this.$confirm({
        title: '删除?',
        content: '是否确定删除该轮播图?',
        onOk: async () => {
          let { code, msg } = await delConfigBanner(id)
          _this.$message[code === 0 ? 'success' : 'error'](msg)
          if (code === 0) {
            await _this.get_config_banner()
            this.handleCarouselChange(index)
          }
        },
        onCancel() {},
      })
    },
  },
  async created() {
    await this.get_config_banner()
    if (this.bannerList.length) {
      this.form.id = this.bannerList[0].id
      this.form.imgUrl = this.bannerList[0].imgUrl
      this.typeOptionsId = this.bannerList[0].typeOptionsId
      this.optionsId = this.bannerList[0].optionsId
      this.optionsName = this.bannerList[0].optionsName
      this.linkUrl = this.bannerList[0].linkUrl
      this.appId = this.bannerList[0].appId
    }
  },
  mounted() {},
}
</script>
<style scoped lang="less">
.warp {
  width: 500px;

  .banner {
    position: relative;

    .img {
      width: 100%;
      height: 160px;
      object-fit: contain;
    }

    .del {
      position: absolute;
      top: 10px;
      right: 10px;
      cursor: pointer;
      width: 20px;
      height: 20px;
    }
  }
}

::v-deep {
  .ant-carousel .slick-slide {
    text-align: center;
    height: 160px;
    line-height: 160px;
    background: #364d79;
    overflow: hidden;
  }

  .ant-carousel .custom-slick-arrow {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    opacity: 0.3;
  }

  .ant-carousel .custom-slick-arrow:before {
    display: none;
  }

  .ant-carousel .custom-slick-arrow:hover {
    opacity: 0.5;
  }

  .ant-carousel .slick-slide h3 {
    color: #fff;
  }
}
</style>